package br.ufrj.dcc.smarthotel.activities;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import br.ufrj.dcc.smarthotel.R;
import br.ufrj.dcc.smarthotel.Util;
import br.ufrj.dcc.smarthotel.controls.EventsMenuControl;
import br.ufrj.dcc.smarthotel.entities.Event;
import br.ufrj.dcc.smarthotel.listeners.EventItemClickListener;

public class EventsMenuActivity extends AppActivity {
	 private EventsMenuControl control;
	 private ListView listEvents;
	 private EventItemClickListener itemClickListener;
	 private List<Event> eventsList;

	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.eventsmenu);
        
        this.findAllViews();
        this.control = new EventsMenuControl(this);
        this.itemClickListener = new EventItemClickListener(this);
        
        this.listEvents.setOnItemClickListener(this.itemClickListener);
        
        this.control.LoadEvents();
    }
	
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		this.control.LoadEvents();
	}

	public void setEventsList(List<Event> list) {
		this.eventsList = list;
		this.listEvents.setAdapter(new EventsListAdapter(this, R.id.listEvents, list));
	}
	
	public List<Event> getEventsList() {
		return this.eventsList;
	}
	
	private void findAllViews() {
		this.listEvents = (ListView) this.findViewById(R.id.listEvents);
	}
	
	class EventsListAdapter extends ArrayAdapter<Event> {
		List<Event> eventsList;
		
		public EventsListAdapter(Context context, int textViewResourceId,
				List<Event> objects) {
			super(context, textViewResourceId, objects);
			
			this.eventsList = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            
	            v = vi.inflate(R.layout.eventitem_list, null);
	        }
	        
	        Event e = eventsList.get(position);
	        
	        if (e != null) {
                TextView txtEventItemTitle = (TextView) v.findViewById(R.id.txtEventItemTitle);
                TextView txtEventItemDate = (TextView) v.findViewById(R.id.txtEventItemDate);
                TextView txtEventItemNumberOfPeople = (TextView) v.findViewById(R.id.txtEventItemNumberOfPeople);
                ImageView imgEventItemIcon = (ImageView) v.findViewById(R.id.imgEventItemIcon);
                
                txtEventItemTitle.setText(e.getName());
                txtEventItemDate.setText(Util.getInstance(getContext()).writeDateTime(e.getStartDate()));
                txtEventItemNumberOfPeople.setText(getString(R.string.lbl_NumberOfAttendants, control.getNumberOfAttendants(e)));
                imgEventItemIcon.setImageBitmap(Util.getInstance(getApplicationContext()).downloadImage(e.getPictureUrl()));
	        }
	        return v;
		}

		
	}
}
